layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func','index'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var index = layui.index;

    /**
     * 流程管理--流程模型
     */
    var FlowModel = {
        tableId: "flowModelTable",    //表格id
        condition: {
            modelName: "",
            key:""
        }
    };
    /**
     * 初始化表格的列
     */
    FlowModel.initColumn = function () {
        return [[
            {field: 'id',  sort: true, width:100, title: 'ID'},
            {field: 'key',  sort: true, title: 'KEY'},
            {field: 'name', align: "center", title: '模型名称'},
            {field: 'version', align: "center",  width:100,title: 'version'},
            {field: 'metaInfo', align: "center", title: '元数据'},
            {field: 'createTime', align: "center", sort: true, title: '创建时间'},
            {field: 'lastUpdateTime', align: "center", sort: true, title: '最后更新时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 250}
        ]];
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FlowModel.tableId,
        url: Feng.ctxPath + '/flowModel/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: FlowModel.initColumn()
    });

    /**
     * 点击查询按钮
     */
    FlowModel.search = function () {
        var queryData = {};
        queryData['name'] = $("#modelName").val();
        queryData['key'] = $("#KEY").val();
        table.reload(FlowModel.tableId, {
            where: queryData, page: {curr: 1}
        });
    };
    
    $("#btnAdd").click(function () {
        FlowModel.openAddModel();
    })


    $("#btnSearch").click(function () {
        FlowModel.search();
    })

    /**
     * 弹出添加角色
     */
    FlowModel.openAddModel = function () {
        func.open({
            height: 470,
            title: '添加流程模型',
            content: Feng.ctxPath + '/flowModel/toCreateModelHTML',
            tableId: FlowModel.tableId
        });
    };

    FlowModel.delete = function(data){
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowModel/delete", function () {
                Feng.success("删除成功!");
                table.reload(FlowModel.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("modelId", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除流程模型 " + data.name + "?", operation);
    }

    FlowModel.deployFlowModel = function (data){
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowModel/deployFlowModel/"+data.id, function () {
                Feng.success("部署成功!");
                table.reload(FlowModel.tableId);
            }, function (data) {
                Feng.error("部署失败!" + data.responseJSON.message + "!");
            });
            ajax.start();
        };
        Feng.confirm("是否部署流程模型 " + data.name + "?", operation);
    }
    // 工具条点击事件
    table.on('tool(' + FlowModel.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        console.log(data)
        if (layEvent === 'edit') {
            index.openTab({url:"/modeler/modeler.html?modelId=" + data.id,title: "模型在线设计"});
        } else if (layEvent === 'delete') {
            FlowModel.delete(data);
        } else if (layEvent === 'deploy') {
            FlowModel.deployFlowModel(data);
        } else if (layEvent === 'export') {
        }
    });

});
